//
//  MyPayViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/4/6.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "MyPayViewController.h"
#import "Order.h"
#import "APAuthV2Info.h"
#import "DataSigner.h"
#import <AliPaySDK/AlipaySDK.h>
@interface MyPayViewController ()

@end

@implementation MyPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    payButton.frame = CGRectMake(10, 100, self.view.frame.size.width-20, 40);
    [payButton setTitle:@"pay 0.01元" forState:UIControlStateNormal];
    [payButton setTitle:@"pay 0.01元" forState:UIControlStateSelected];
    [payButton setBackgroundColor:[UIColor orangeColor]];
    [payButton addTarget:self action:@selector(payAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payButton];
}

/**
 *  支付按钮
 */
- (void)payAction
{
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088*********"; /** 合作商户ID  PID */
    NSString *seller = @"zhuchunfeng@juduo.com"; /** 支付宝账号ID  */
    NSString *privateKey = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALNnlKlHeonmnbODQ6cGhr0Ln/FWifCVI2qKkqEBvWLcBwtO+Z+MnALThOu/r4DgoYyfGq3oSQAQmqKSlX4cuYGXXt46NvRCApsXpRHXUpDPruekRliX4T0zu+En/b9map01NcBOrgeWPpBMYJVf+gTcfrFUX1P1C6eVTxeb3N5zAgMBAAECgYAuQIYTHlp5Mvt1TGIkrYoPMvAJSEZHwCthTFiBKfGpixBF2haBC78s+1tJbCHTGBaOKna2Xyxy19UGFMR1vvIUCGscxoWaO5YvHT5tjZf+8UOGREnbXBysqHNtJzI7udwq//z2iqsegmD4S2h/SrUkD5bU4FC2guwhadRJG97gAQJBANsLTzE80VjrGGPjefYBnKfh6F/SCq30zX2TZ7KI7ri65p6l+KsAYaSk46ZSm4OJwbQW/wktPicMjCPZ30twJBkCQQDRrDUSJzdBkgPcw158Yjfk2rODPYWUtgAeZjUtOSJvQ4S0/IeGE8/ryj1uSPclV0+xNbB9DOQNeL0Mw/adZQhrAkA9ZC4N3r3N7hYn6NmF2W3On8JBGN1lY/cz+txVbYt6uudoD5xiUPkM7Cnhs8ViWHmxv8A7EjMZPqsIYUf0HLoBAkEAjP8X5TA36UEulbtPg6ds3NnxE5VAigNlywuCQPoF3PpzeF3FKPWTwYULkGlCwXqYXZu0JjOP/DswLjBtW+SAJwJAbS61Mg69yGf7YmB//Ff3M6P+KlfyxfEt9Dz/9WB7TR7xaoTCv/KE8dIo4IAKON9Zcwo3vkCcPZYRqVzW5DAt6Q==";
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = [self generateTradeNO]; //订单ID（由商家自行制定）
    order.productName = @"runbey测试商品"; //商品标题
    order.productDescription = @"runbey商品描述"; //商品描述
    order.amount = @"0.01"; //商品价格
    order.notifyURL =  @"http://*****"; //回调URL 异步通知服务器支付结果，便于服务器更新订单状态
    
    //实际项目开发中 订单信息应该由服务器返回
    
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme 便于支付结束后重新打开客户端
    NSString *appScheme = @"ybpaydemo";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        NSLog(@"签名字符串:%@",orderString);
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"支付结果reslut = %@",resultDic);
        }];
    }
}

#pragma mark -
#pragma mark   ==============产生随机订单号==============

- (NSString *)generateTradeNO
{
    static int kNumber = 15;
    
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}

@end
